"""Header_Information
------------------------------------
    File_Name: models
    Author: RR9
    Date: 2024/09/18
    Description: Pydantic数据模型和SQLAlchemy数据模型定义
------------------------------------
"""

from tortoise.models import Model
from tortoise import fields

"""
ob1
"""


class Student(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description="姓名")
    pwd = fields.CharField(max_length=60, description="密码")
    sno = fields.IntField(description="学号")

    # 一对多关系，一个学生对应多个班级
    # ⚠️注意：生成的是field_id 字段，即 clas_id
    clas = fields.ForeignKeyField("models.Clas", related_name="students")

    # 多对多，一个学生可以有多门课程，一门课程也可以有多个学生
    # ⚠️注意：会额外生成一个关联表（field1_field2）， 即 student_course
    courses = fields.ManyToManyField("models.Course", related_name="students")

    def __str__(self):
        """打印时"""
        return f"<Student: {self.name}>"

    def __repr__(self):
        """调用时"""
        return f"<Student(id = {self.id}, name = {self.name})>"


class Course(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description="课程名称")
    teacher = fields.ForeignKeyField("models.Teacher", related_name="course")
    addr = fields.CharField(max_length=32, description="教室", default="")
    type = fields.CharField(max_length=2, description="是否校内", default="Y")

    def __str__(self):
        """打印时"""
        return f"<Course: {self.name}>"

    def __repr__(self):
        """调用时"""
        return f"<Course(id = {self.id}, name = {self.name})>"


class Clas(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description="班级名称")

    def __str__(self):
        """print打印时"""
        return f"<Clas: {self.name}>"

    def __repr__(self):
        """调用时"""
        return f"<Clas(id = {self.id}, name = {self.name})>"


class Teacher(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=32, description="姓名")
    pwd = fields.CharField(max_length=32, description="密码")
    tno = fields.IntField(description="老师编号")

    def __str__(self):
        """打印时"""
        return f"<Teacher: {self.name}>"

    def __repr__(self):
        """调用时"""
        return f"<Teacher(id = {self.id}, name = {self.name})>"


"""
ob2
"""
